<?php echo $javascript->link('filter_actions'); ?>
<div class="jobs index">
    <a href="" class="filtro_a" onclick="show_filter(); return false;"><?php echo __('Filtro', true); ?></a>
    <input type="hidden" id="input_filtro" value="0">
    <div id="filtro_busqueda" class="filtro" style="display: block;">
        <fieldset>
            <?php echo $form->create('Future', array('url' => array('controller' => 'futures', 'action' => 'index'))); ?>
            <div class="date_range">
                <label>Rango de Fechas</label>
                <?php
                echo $form->input('date_from', array('label' => false));
                ?>
                <span>-</span>
                <?php
                echo $form->input('date_to', array('label' => false));
                ?>
                <span><?php echo $this->Html->link(__('Hoy', true), array(), array('onclick' => 'fecha_hoy(); return false;')); ?></span>
                &nbsp;&nbsp;&nbsp;
                <span><?php echo $this->Html->link(__('Próximos 7 Días', true), array(), array('onclick' => 'fecha_semana(); return false;')); ?></span>
                <?php
                echo $form->input('hidden_actual_date', array('type' => 'hidden', 'value' => date('d/m/Y')));
                echo $form->input('hidden_week_date', array('type' => 'hidden', 'value' => date('d/m/Y', strtotime('+7 day', strtotime(date('Y-m-d H:i:s'))))));
                ?>
            </div>
            <?php echo $form->input('pelotero_id', array('label' => 'Pelotero', 'empty' => __('Todos', true))); ?>
            <?php
            echo $form->submit(__('Filtrar', true));
            echo $form->end();
            ?>
        </fieldset>
    </div>
    <div id="div_imprimir">
        <?php
//        echo $this->Html->link(
//        $this->Html->image('impresora.png', array('width'=>40, 'alt'=>'imprimir')),
//        array('controller' => 'futures', 'action' => 'imprimir'),
//        array('escape' => false)
//        );
        echo $this->Html->link(
                $this->Html->image('impresora.png', array('width' => 40, 'alt' => 'imprimir')), array('controller' => 'futures', 'action' => 'imprimir'), array('escape' => false, 'target' => '_blank')
        );
        ?>
        <?php //echo $this->Html->link(__('Imprimir', true), array('controller' => 'theaters', 'action' => 'index')); ?>
    </div>
    <h2 class="left"><?php __('Cumpleaños / Eventos / Obras de Teatro'); ?></h2>
    <div class="actions specific right">
        <ul>
            <li><?php echo $this->Html->link(__('Agregar Cumple/Evento', true), array('controller' => 'jobs', 'action' => 'add')); ?> </li>
            <li><?php echo $this->Html->link(__('Agregar Obra de Teatro', true), array('controller' => 'theaters', 'action' => 'add')); ?> </li>
        </ul>
    </div>
    <div class="clear"></div>
    <p><b>Cantidad de Trabajos: </b><?php echo $cantidad_total_trabajos; ?></p>
    <table cellpadding="0" cellspacing="0" class="table_jobs">
        <tr>
            <th><?php echo ''; ?></th>
            <th class="actions"><?php __('Acciones'); ?></th>
        </tr>
        <?php
        if (empty($array_todo)) {
            ?>
            <tr>
                <td colspan="2">No se encontraron trabajos para el filtro aplicado</td>
            </tr>
            <?php
        }
        $tipos_tareas = array('show' => 'Show', 'reception' => 'Presentación', 'animation' => 'Animación', 'production' => 'Obra', 'service' => 'Servicios');
        $i = 0;
        foreach ($array_todo as $todo) {


            if (isset($todo['Theaters'])) {

                //EMPIEZA LA PARTE DE THEATERS

                $empleados_usados = array();
                $trajes_usados = array();
                foreach ($todo['Theaters'] as $theater) {
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }
                    ?>
                    <tr<?php echo $class; ?>>
                        <td>
                            <b>Obra de Teatro: </b><?php echo $theater['Theater']['name'] ?>
                            <br />
                            <?php
                            echo date('d/m/Y', strtotime($theater['Theater']['start_date']));
                            echo '&nbsp;&nbsp;&nbsp;&nbsp;';
                            echo date('H:i', strtotime($theater['Theater']['start_date'])) . ' - ' . date('H:i', strtotime($theater['Theater']['finish_date']));
                            ?>
                            <br />
                            <?php
                            echo '<b>Dirección: </b>' . $theater['Theater']['address'];
                            echo '&nbsp; <b>Teléfonos:</b> ';
                            echo $theater['Theater']['phone_number'];
                            echo ' - ';
                            echo $theater['Theater']['movil_number'];

                            if (isset($theater['Theater']['TheatersEmployee'])) {
                                foreach ($theater['Theater']['TheatersEmployee'] as $emp) {
                                    if (!empty($emp['employee_id'])) {
                                        $empleados_usados[$emp['employee_id']] = $employees[$emp['employee_id']];
                                    }
                                }
                            }
                            if (isset($theater['Theater']['TheatersCostume'])) {
                                foreach ($theater['Theater']['TheatersCostume'] as $costume) {
                                    if (!empty($costume['costume_id'])) {
                                        $trajes_usados[$costume['costume_id']] = $costumes[$costume['costume_id']];
                                    }
                                }
                            }
                            echo '<br />';
                            echo '<b>Trajes: </b>' . implode(', ', $trajes_usados);
                            echo '<br />';
                            echo '<b>Empleados: </b>' . implode(', ', $empleados_usados);
                            if ($theater['Theater']['description'] != '') {
                                echo '<br />';
                                echo '<b>Notas: </b>' . $theater['Theater']['description'];
                            }
                            ?>
                        </td>
                        <td class="actions">
                            <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller' => 'theaters', 'action' => 'view', $theater['Theater']['id']), array('title' => __('Ver', true), 'escape' => false)); ?>
                            <?php echo $this->Html->link($this->Html->image('icons/pencil.png'), array('controller' => 'theaters', 'action' => 'edit', $theater['Theater']['id']), array('title' => __('Editar', true), 'escape' => false)); ?>
                            <?php echo $this->Html->link($this->Html->image('icons/cross.png'), array('controller' => 'theaters', 'action' => 'delete', $theater['Theater']['id']), array('title' => __('Eliminar', true), 'escape' => false), sprintf('Seguro que desea eliminar esta Obra de Teatro?', $theater['Theater']['id'])); ?>
                        </td>
                    </tr>
                    <?php
                }
            }

            //TERMINA LA PARTE DE THEATERS
            //si tiene trabajos
            if (isset($todo['Jobs'])) {

                //EMPIEZA LA PARTE DE JOBS

                $empleados_usados = array();
                $trajes_usados = array();
                foreach ($todo['Jobs'] as $job):
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }

                    //voy a armar un array con los shows, uno con las recepciones, animació, servicios y obras
                    $array_cosas = array();
                    foreach ($job['Job']['JobsDetail'] as $jobs_detail) {
                        $tipo = $jobs_detail['type'];
                        $array_cosas[$tipo][$jobs_detail['id']] = $jobs_detail;
                    }
                    ?>
                    <tr<?php echo $class; ?>>
                        <td>
                            <?php
                            $edad = $job['Job']['age'] != NULL ? $job['Job']['age'] . ' años' : '';
                            $tipo = $job['Job']['type'] == 'event' ? 'Evento: ' . $job['Job']['event_name'] : 'Cumpleaños: ' . $job['Job']['birthday_name'] . ' ' . $edad;
                            ?>
                            <b><?php echo $tipo; ?></b>
                            <?php echo isset($peloteros[$job['Job']['pelotero_id']]) ? '&nbsp;&nbsp;||&nbsp;&nbsp;<b>Pelotero: </b>' . $peloteros[$job['Job']['pelotero_id']] : ''; ?>
                            <br />
                            <?php
                            echo date('d/m/Y', strtotime($job['Job']['start_date']));
                            echo '&nbsp;&nbsp;&nbsp;&nbsp;';
                            echo date('H:i', strtotime($job['Job']['start_date'])) . ' - ' . date('H:i', strtotime($job['Job']['finish_date']));
                            ?>
                            <br />
                            <?php
                            echo '<b>Dirección: </b>' . $job['Job']['address'];
                            echo '&nbsp; <b>Teléfonos:</b> ';
                            echo $job['Job']['phone_number'];
                            echo ' - ';
                            echo $job['Job']['movil_number'];
                            ?>
                            <br />
                            <?php
                            if ($job['Job']['count_invited'] != NULL) {
                                ?>
                                <p><b>Cant. Invitados: </b><?php echo $job['Job']['count_invited'] ?></p>
                                <?php
                            }

                            $texto_salida = '';
                            $empleados_usados = array();
                            $trajes_usados = array();
                            foreach ($array_cosas as $tarea_type => $tarea) {
                                ?>
                                <div class="job_detail_<?php echo $tarea_type ?>">
                                    <?php
                                    $texto_salida = '<b>' . $tipos_tareas[$tarea_type] . ': </b>';
                                    $aux_cant_emp = 0;
                                    foreach ($tarea as $tar) {
                                        $texto_salida .=!empty($tar['show_id']) ? $shows[$tar['show_id']] : '';
                                        $texto_salida .=!empty($tar['service_id']) ? $services[$tar['service_id']] . ', ' : '';
                                        $texto_salida .=!empty($tar['production_id']) ? $productions[$tar['production_id']] : '';
                                        $texto_salida .=!empty($tar['count_invited']) ? $tar['count_invited'] . ' invitados' : '';

                                        foreach ($tar['JobsDetailsEmployee'] as $emp) {
                                            if (!empty($emp['employee_id'])) {
                                                $empleados_usados[$emp['employee_id']] = $employees[$emp['employee_id']];
                                                if ($tarea_type == 'animation' || $tarea_type == 'show' || $tarea_type == 'production') {
                                                    $aux_cant_emp++;
                                                }
                                            } elseif (isset($emp['cost'])) {
                                                //si no esta seteado el id del empleado, pero si el costo
                                                if ($tarea_type == 'animation' || $tarea_type == 'show' || $tarea_type == 'production') {
                                                    $aux_cant_emp++;
                                                }
                                            }
                                            if (!empty($emp['costume_id'])) {
                                                //si es una recepcion anoto el nombre de los trajes
                                                if ($tarea_type == 'reception') {
                                                    $texto_salida .= $costumes[$emp['costume_id']] . ', ';
                                                }
                                                $trajes_usados[$emp['costume_id']] = $costumes[$emp['costume_id']];
                                            }
                                        }
                                        if ($tarea_type == 'animation' || $tarea_type == 'show' || $tarea_type == 'production') {
                                            $texto_salida .= '(' . $aux_cant_emp . ' P), ';
                                        }
                                    }
                                    //me fijo si termina con una coma
                                    $texto_salida = substr($texto_salida, -2, 2) == ', ' ? substr($texto_salida, 0, -2) : $texto_salida;
                                    echo $texto_salida;
                                    ?>
                                    <br />
                                </div>
                                <?php
                            }
                            echo '<b>Trajes: </b>' . implode(', ', $trajes_usados);
                            echo '<br />';
                            echo '<b>Empleados: </b>' . implode(', ', $empleados_usados);
                            echo '<br />';
                            echo '<b>Costo: </b>$' . $job['Job']['total_cost'];
                            echo '<br />';
                            echo $job['Job']['sign'] != NULL ? '<b>Seña: </b>$' . $job['Job']['sign'] : '';
                            if ($job['Job']['description'] != '') {
                                echo '<br />';
                                echo '<b>Notas: </b>' . $job['Job']['description'];
                            }
                            ?>
                        </td>
                        <td class="actions">
                            <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller' => 'jobs', 'action' => 'view', $job['Job']['id']), array('title' => __('Ver', true), 'escape' => false)); ?>
                            <?php echo $this->Html->link($this->Html->image('icons/pencil.png'), array('controller' => 'jobs', 'action' => 'edit', $job['Job']['id']), array('title' => __('Editar', true), 'escape' => false)); ?>
                            <?php echo $this->Html->link($this->Html->image('icons/cross.png'), array('controller' => 'jobs', 'action' => 'delete', $job['Job']['id']), array('title' => __('Eliminar', true), 'escape' => false), sprintf('Seguro que desea eliminar este ' . $tipo . '?', $job['Job']['id'])); ?>
                            <?php
                            if ($job['Job']['paid'] == 'yes') {
                                ?>
                                <div class="paid_job">Pagado</div>
                                <?php
                            }
                            ?>
                        </td>
                    </tr>
                    <?php
                endforeach;
                //TERMINA LA PARTE DE JOBS
            }

            //EMPIEZA LA PARTE DE ANOTATIONS
            //si tiene anotaciones
            if (isset($todo['Anotations'])) {
                foreach ($todo['Anotations'] as $anotation) {
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }
                    ?>
                    <tr<?php echo $class; ?>>
                        <td>
                            <b>Anotacion</b>
                            <br />
                            <?php
                            echo date('d/m/Y', strtotime($anotation['Anotation']['start_date']));
                            echo '&nbsp;&nbsp;&nbsp;&nbsp;';
                            echo date('H:i', strtotime($anotation['Anotation']['start_date'])) . ' - ' . date('H:i', strtotime($anotation['Anotation']['finish_date']));
                            ?>
                            <br />
                            <?php
                            echo $anotation['Anotation']['description'];
                            ?>
                        </td>
                        <td class="actions">
                            <?php echo $this->Html->link($this->Html->image('icons/arrow_refresh.png'), array('controller' => 'anotations', 'action' => 'create', $anotation['Anotation']['id']), array('title' => __('Transformar', true), 'escape' => false)); ?>
                            <?php echo $this->Html->link($this->Html->image('icons/pencil.png'), array('controller' => 'anotations', 'action' => 'edit', $anotation['Anotation']['id']), array('title' => __('Editar', true), 'escape' => false)); ?>
                            <?php echo $this->Html->link($this->Html->image('icons/cross.png'), array('controller' => 'anotations', 'action' => 'delete', $anotation['Anotation']['id']), array('title' => __('Eliminar', true), 'escape' => false), sprintf('Seguro que desea eliminar esta Anotación?', $anotation['Anotation']['id'])); ?>
                        </td>
                    </tr>
                    <?php
                }
            }
            //TERMINA LA PARTE DE ANOTATIONS
        }
        ?>

    </table>
</div>
<div id="resultado_imprimir"></div>
<script type="text/javascript" >

    (function($){
        // Date picker
        $("#FutureDateFrom").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });
        // Date picker
        $("#FutureDateTo").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

    })(jQuery);

    function fecha_hoy(){
        var hoy = $('#FutureHiddenActualDate').val();
        $('#FutureDateFrom').val(hoy);
        $('#FutureDateTo').val(hoy);
        $('#FutureIndexForm').submit();
    }

    function fecha_semana(){
        var hoy = $('#FutureHiddenActualDate').val();
        var semana = $('#FutureHiddenWeekDate').val();
        $('#FutureDateFrom').val(hoy);
        $('#FutureDateTo').val(semana);
        $('#FutureIndexForm').submit();
    }

</script>